package cn.qiyu5522.tk_ems.controller.admin;


import cn.qiyu5522.tk_ems.entity.Job;
import cn.qiyu5522.tk_ems.entity.dto.JobDTO;
import cn.qiyu5522.tk_ems.entity.dto.QueryParamDTO;
import cn.qiyu5522.tk_ems.myanotation.WebLog;
import cn.qiyu5522.tk_ems.result.R;
import cn.qiyu5522.tk_ems.service.JobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Tonkey
 * @since 2023-02-20
 */
@Slf4j
@Api(tags = "岗位管理")
@RestController
@RequestMapping("admin/job")
// 查询用户是否为企业
//@SaAdminCheckRole
// 检查用户是否管理员
//@SaCompanyCheckRole
public class JobController {

    @Resource
    private JobService jobService;

    @WebLog(description = "分页条件查询工作列表")
    @ApiOperation("工作列表查询")
    @ApiImplicitParam(name = "queryParamDTO", value = "条件查询对象", required = true,dataType ="QueryParamDTO" ,dataTypeClass = QueryParamDTO.class)
    @PostMapping("/query")
    public R query(@Validated(QueryParamDTO.Query.class) @RequestBody QueryParamDTO<JobDTO> queryParamDTO) {
        return jobService.queryJobsByCondition(queryParamDTO);
    }
    @WebLog(description = "工作新增--批量")
    @ApiOperation("工作新增")
    @ApiImplicitParam(name = "JobDTOs", value = "工作新增", required = true,dataType = "List",dataTypeClass = List.class)
    @PostMapping("/add")
    public R add(@Validated(Job.Insert.class) @RequestBody List<JobDTO> JobDTOs) {
        return jobService.saveMore(JobDTOs);
    }
    @WebLog(description = "工作编辑")
    @ApiOperation("工作编辑")
    @ApiImplicitParam(name = "jobDTO", value = "工作实体类对象", required = true, defaultValue = "{}", dataTypeClass = JobDTO.class)
    @PutMapping("/edit")
    public R edit(@Validated(Job.Update.class) @RequestBody JobDTO jobDTO) {
        Job job = new Job();
        BeanUtils.copyProperties(jobDTO,job);
        boolean b = jobService.updateById(job);
        return b ? R.ok().message("编辑成功") : R.error().message("编辑失败");
    }
    @WebLog(description = "工作删除")
    @ApiOperation("工作删除")
    @ApiImplicitParam(name = "ids", value = "工作编号集合", required = true, defaultValue = "[]", dataTypeClass = List.class)
    @PostMapping("/remove")
    public R remove(@RequestBody List<Long> ids) {
        boolean b = jobService.removeByIds(ids);
        return b ? R.ok().message("删除成功!"):R.error().message("删除失败");
    }

    @ApiOperation("根据ID查询岗位信息")
    @ApiImplicitParam(name = "id", value = "岗位表主键", required = true, dataTypeClass = Integer.class)
    @GetMapping("/queryJobInfoById/{id}")
    public R queryJobInfoById(@PathVariable Integer id) {
        return jobService.queryJobInfoById(id);
    }

}
